﻿/******************************************************************************
'Description: DEC加密及解密URL参数工具类
'Copyright (c) : 通力凯顿（北京）系统集成有限公司
'Writer:zhunan
'create Date:2018.12.26
 ********************************************************************************/
using System;
using System.Text;
using System.IO;
using System.Security.Cryptography;
using System.Web;
using System.Collections.Generic;
namespace uMES.LeanManufacturing.Common
{
    public class EncryptAndDecryptTool
    {
        public static string encryptKey = "mesreport";//加密秘钥、解密秘钥
        /// <summary>
        /// 加密方法
        /// </summary>
        /// <param name="encryptString">要加密的字符串</param>
        /// <returns>经过加密的字符串</returns>
        public static string DesEncrypt(string encryptString)
        {
            try
            {
                byte[] keyBytes = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8));
                byte[] keyIV = keyBytes;
                byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);
                DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
                MemoryStream mStream = new MemoryStream();
                CryptoStream cStream = new CryptoStream(mStream, provider.CreateEncryptor(keyBytes, keyIV), CryptoStreamMode.Write);
                cStream.Write(inputByteArray, 0, inputByteArray.Length);
                cStream.FlushFinalBlock();
                return Base64StringToAscii(Convert.ToBase64String(mStream.ToArray()));
            }
            catch (Exception)
            {
                return "";
                throw;
            }
        }

        /// <summary>
        /// 将加密后base64字符转换为ASCII码
        /// </summary>
        /// <param name="Base64String"></param>
        /// <returns></returns>
        public static string Base64StringToAscii(string Base64String)
        {
            StringBuilder sbAsciiString = new StringBuilder();

            byte[] bTemp = System.Text.Encoding.Default.GetBytes(Base64String);

            for (int i = 0; i < bTemp.Length; i++)
            {
                sbAsciiString.Append(bTemp[i].ToString("X2"));
            }
            return sbAsciiString.ToString();
        }

        /// <summary>
        /// 将ASCII码转换为base64字符串
        /// </summary>
        /// <param name="strASCII">16进制ASCII码字符串</param>
        /// <returns></returns>
        public static string AsciiToBase64(string strASCII)
        {
            List<string> hexList = new List<string>();
            for (int i = 0; i < strASCII.Length; i += 2)
            {
                hexList.Add(strASCII.Substring(i, 2));
            }
            StringBuilder sbBase64 = new StringBuilder();
            foreach (string hex in hexList)
            {
                int value = Convert.ToInt32(hex, 16);
                sbBase64.Append(Char.ConvertFromUtf32(value));
            }
            return sbBase64.ToString();
        }

        /// <summary>
        /// 解密方法
        /// </summary>
        /// <param name="decryptString">要进行解密的字符串</param>
        /// <returns>经过机密后的字符串</returns>
        public static string DesDecrypt(string decryptString)
        {
            try
            {
                byte[] keyBytes = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8));
                byte[] keyIV = keyBytes;
                byte[] inputByteArray = Convert.FromBase64String(AsciiToBase64(decryptString));

                DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
                MemoryStream mStream = new MemoryStream();
                CryptoStream cStream = new CryptoStream(mStream, provider.CreateDecryptor(keyBytes, keyIV), CryptoStreamMode.Write);
                cStream.Write(inputByteArray, 0, inputByteArray.Length);
                cStream.FlushFinalBlock();
                return Encoding.UTF8.GetString(mStream.ToArray());
            }
            catch (Exception)
            {
                return "";
                throw;
            }
        }
    }
}
